/* Includes ------------------------------------------------------------------*/
#include "TimeEvent.h"
#include "display.h"
#include "comp.h"
#include "ad_temp.h"
#include "var.h"
// #include "Variable.h"

// #include "TimeEvent.h"
// #include "ThermostatTask.h"
// #include "tVariable.h"
// #include "display.h"

// #include "bsp_periph.h"
/*********************************************************************
 * MACROS
 */
/*********************************************************************
 * TYPEDEFS
 */

/*********************************************************************
 * CONSTANTS
 */

/*********************************************************************
 * GLOBAL VARIABLES
 */

Event_Timer idata timer_10msDB[TimeEventSize];

/*********************************************************************
 * EXTERNAL VARIABLES
 */

/*********************************************************************
 * EXTERNAL FUNCTIONS
 */

/*********************************************************************
 * LOCAL VARIABLES
 */
void Timer_Event_Set(Event_Timer *timer, uint32_t set_time, uint8_t enable, uint8_t flag, void (*func)(void));
void Init_Timer_Event(void);
void TimerEvent_Handle(void);
void TimerEvent_Ticks(void);

/*********************************************************************
 * @fn      void Timer_Event_Set( Event_Timer *timer, uint32 set_time, uint8 enable, uint8 flag, void(*func)(void))
 *
 * @brief   set time event
 *
 * @param  timer
 * @param  set_time
 * @param  enable
 * @param  flag
 * @param  *func
 *
 * @return  none
 */
void Timer_Event_Set(Event_Timer *timer, uint32_t set_time, uint8_t enable, uint8_t flag, void (*func)(void))
{
	timer->Time = set_time;
	timer->Enable = enable;
	timer->Flag = flag;
	timer->TimersHandle = func;
}

/*********************************************************************
 * @fn      void Set_Event( Event_Timer *timer,  uint8 enable, uint8 flag, void(*func)(void) )
 *
 * @brief   set time event
 *
 * @param  timer
 * @param  set_time
 * @param  enable
 * @param  flag
 * @param  *func
 *
 * @return  none
 */
// void Set_Event(Event_Timer *timer, void (*func)(void))
// {
// 	timer->Time = 0;
// 	timer->Enable = 0;
// 	timer->Flag = 1;
// 	timer->TimersHandle = func;
// }

/*********************************************************************
 * @fn       void Init_Timer_Event(void)
 *
 * @brief    init time event
 *
 *
 *
 * @param  none
 * @param  none
 *
 *
 * @return  none
 */

// void CompressorStateSymbolRefurbish(void)
//{
//	timer_10msDB[0].Flag = 0;
//	timer_10msDB[0].Enable = ENABLE;
//	timer_10msDB[0].Time = 500;//100ms 轮询

//	P1_5=~P1_5;
//}
void Init_Timer_Event(void)
{
	Timer_Event_Set(&timer_10msDB[NtcEvt_TaskId], 100, ENABLE, 0, Adc_Ntc_Evt);
	Timer_Event_Set(&timer_10msDB[SymbolRefurbish_TaskId], 2000, ENABLE, 0, SymbolRefurbish);
	Timer_Event_Set(&timer_10msDB[DisplayRefurbish_TaskId], 2000, ENABLE, 0, DisplayRefurbish);

	Timer_Event_Set(&timer_10msDB[CompCtr_TaskId], 1000, ENABLE, 0, CompressorControlEvt);
	Timer_Event_Set(&timer_10msDB[CompDefsort_TaskId], 102, ENABLE, 0, CompressorDefrosteDectEvt);
}
/*********************************************************************
 * @fn       void Init_ThermostatTask(void)
 *
 * @brief    init time event
 *
 *
 *
 * @param  none
 * @param  none
 *
 *
 * @return  none
 */
//void Init_ThermostatTask(void)
//{
//}
/*********************************************************************
 * @fn      Timer_Event_Handle
 *
 * @brief
 *
 *
 *
 * @param  none
 * @param  none
 *
 *
 * @return  none
 */
void TimerEvent_Handle(void)
{
	for (evt_index = 0; evt_index < TimeEventSize; evt_index++)
	{
		if (timer_10msDB[evt_index].Flag)
		{
			if (timer_10msDB[evt_index].TimersHandle != 0)
			{
				timer_10msDB[evt_index].TimersHandle();
			}
		}
	}
}
/*********************************************************************
 * @fn      TimerEvent_Ticks
 *
 * @brief   10ms
 *
 *
 *
 * @param  none
 * @param  none
 *
 *
 * @return  none
 */

void TimerEvent_Ticks(void)
{

	for (Evt_tick = 0; Evt_tick < TimeEventSize; Evt_tick++)
	{
		if (timer_10msDB[Evt_tick].Enable)
		{
			if (timer_10msDB[Evt_tick].Time > 0)
			{
				timer_10msDB[Evt_tick].Time--;
			}
			else
			{
				timer_10msDB[Evt_tick].Enable = Disable;
				timer_10msDB[Evt_tick].Time = 0;
				timer_10msDB[Evt_tick].Flag = 1;
			}
		}
	}
}
